const baseUrl = "http://localhost:8080"
function request(req){
  if(req.header==undefined){
    req.header = {}
  }
  req.header.token = wx.getStorageSync('token')
  return new Promise((resolve,reject)=>{
    wx.request({
      url: baseUrl+(req.url??""),
      method:req.method??"GET",
      data:req.data,
      header:req.header,
      success:(res)=>{resolve(res)},
      fail:(res)=>{reject(res)}
    })
  })
}
function get(req){
  if(req.header==undefined){
    req.header = {}
  }
  req.header.token = wx.getStorageSync('token')
  return new Promise((resolve,reject)=>{
    wx.request({
      url: baseUrl+(req.url??""),
      method:"GET",
      header:req.header,
      success:(res)=>{resolve(res)},
      fail:(res)=>{reject(res)}
    })
  })
}
function post(req){
  if(req.header==undefined){
    req.header = {}
  }
  req.header.token = wx.getStorageSync('token')
  return new Promise((resolve,reject)=>{
    wx.request({
      url: baseUrl+(req.url??""),
      method:"POST",
      data:req.data,
      header:req.header,
      success:(res)=>{resolve(res)},
      fail:(res)=>{reject(res)}
    })
  })
}
function put(req){
  if(req.header==undefined){
    req.header = {}
  }
  req.header.token = wx.getStorageSync('token')
  return new Promise((resolve,reject)=>{
    wx.request({
      url: baseUrl+(req.url??""),
      method:"PUT",
      data:req.data,
      header:req.header,
      success:(res)=>{resolve(res)},
      fail:(res)=>{reject(res)}
    })
  })
}
function del(req){
  if(req.header==undefined){
    req.header = {}
  }
  req.header.token = wx.getStorageSync('token')
  return new Promise((resolve,reject)=>{
    wx.request({
      url: baseUrl+(req.url??""),
      method:req.method??"GET",
      data:req.data,
      header:req.header,
      success:(res)=>{resolve(res)},
      fail:(res)=>{reject(res)}
    })
  })
}
const http = {request,get,post,put,"delete":del}
module.exports = http